package com.happly.payHapplycloud.Controller;

import com.happly.payHapplycloud.service.impl.OrderPayServiceImpl;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/order")
@Slf4j
public class payController {

    @Autowired
    private OrderPayServiceImpl payService;

    @PostMapping("/create.do")
    public ModelAndView create(@RequestParam("orderId")String orderId,@RequestParam("amount")BigDecimal amount,@RequestParam("payType") BestPayTypeEnum bestPayTypeEnum){

        PayResponse response = payService.createOrder(orderId, amount, bestPayTypeEnum);
        //支付方式不同，渲染就不同, WXPAY_NATIVE使用codeUrl,  ALIPAY_PC使用body
        Map<String, String> map = new HashMap<>();
        if (bestPayTypeEnum == BestPayTypeEnum.WXPAY_NATIVE) {
            map.put("codeUrl", response.getCodeUrl());
            return new ModelAndView("createForWxNative", map);
        }else if (bestPayTypeEnum == BestPayTypeEnum.ALIPAY_PC) {
            map.put("body", response.getBody());
            return new ModelAndView("createForAlipayPc", map);
        }
        throw new RuntimeException("暂不支持的支付类型");
    }

    @PostMapping("/notify.do")
    @ResponseBody
    public void asyncNotify(@RequestParam String notifyData){
        payService.asyncNotify(notifyData);
    }
}
